//
//  DKLoginResetPwdViewController.m
//  DanKU
//
//  Created by 吴斌 on 2023/9/24.
//

#import "DKLoginResetPwdViewController.h"
#import "DKLoginRegisterNameView.h"
#import "DKLoginViewController.h"
#import "DKLoginPwdView.h"
#import "DKLoginAgreementAlertView.h"
@interface DKLoginResetPwdViewController ()<TTTAttributedLabelDelegate>
@property (nonatomic, strong)UIButton * dk_LoginNextBtn;
@property (nonatomic, strong)UIButton * dk_RegisterAgreementBtn;
@property (nonatomic, strong)DKLoginPwdView *dk_resetPwdView;

@property (nonatomic, strong)DKLoginAgreementAlertView * dk_LoginAgreementAlertView;
@end

@implementation DKLoginResetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    // Do any additional setup after loading the view.
    [self dk_creatView];
    [self makeNavBack];
}
- (void)dk_creatView{
    WeakSelf(self)
    //log名称
    DKLoginRegisterNameView *dk_resetPwdNameView = [[DKLoginRegisterNameView alloc] init];
    [self.view addSubview:dk_resetPwdNameView];
    [dk_resetPwdNameView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@(DK_IsWidthScale(0)));
        make.top.equalTo(@(DK_IsWidthScale(76)+DK_SafeAreaTop));
        make.height.equalTo(@(DK_IsWidthScale(40)));
    }];
    //注册提醒
    UILabel *dk_resetPwdAlertLab = [[UILabel alloc] init];
    dk_resetPwdAlertLab.textColor = DK_Hex(@"#333333");
    dk_resetPwdAlertLab.font = DK_FONT_Medium(17);
    dk_resetPwdAlertLab.text = @"Please set your password";
    [self.view addSubview:dk_resetPwdAlertLab];
    [dk_resetPwdAlertLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(24)));
        make.top.equalTo(dk_resetPwdNameView.mas_bottom).offset(DK_IsWidthScale(19));
    }];
    //Captcha登录
    UILabel *dk_resetPhoneLab = [[UILabel alloc] init];
    dk_resetPhoneLab.textColor = DK_Hex(@"#333333");
    dk_resetPhoneLab.font = DK_FONT_Regular(15);
    dk_resetPhoneLab.text = [NSString stringWithFormat:@"+91 %@",self.dk_loginPhone];
    [self.view addSubview:dk_resetPhoneLab];
    [dk_resetPhoneLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(51)));
        make.height.equalTo(@(DK_IsWidthScale(17)));
        make.top.equalTo(dk_resetPwdAlertLab.mas_bottom).offset(DK_IsWidthScale(42));
    }];
    //输入密码
    self.dk_resetPwdView = [[DKLoginPwdView alloc] init];
    [self.view addSubview:self.dk_resetPwdView];
    [self.dk_resetPwdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(dk_resetPhoneLab.mas_bottom).offset(DK_IsWidthScale(30));
        make.height.equalTo(@(DK_IsWidthScale(50)));
    }];
    self.dk_resetPwdView.goCheckLoginPwdBlock = ^(BOOL isPwdNull) {//检查输入密码是否为空
        [weakSelf updataNextStatus];
    };
    
    //协议
    self.dk_RegisterAgreementBtn = [[UIButton alloc] init];
    [self.dk_RegisterAgreementBtn setBackgroundImage:[UIImage imageNamed:@"dk_register_noSelect"] forState:UIControlStateNormal];
    [self.dk_RegisterAgreementBtn setBackgroundImage:[UIImage imageNamed:@"dk_register_Select"] forState:UIControlStateSelected];
    [self.dk_RegisterAgreementBtn addTarget:self action:@selector(goAgreeAgreenment:) forControlEvents:UIControlEventTouchUpInside];
    self.dk_RegisterAgreementBtn.selected = YES;
    [self.view addSubview:self.dk_RegisterAgreementBtn];
    [self.dk_RegisterAgreementBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(36)));
        make.width.equalTo(@(DK_IsWidthScale(16)));
        make.height.equalTo(@(DK_IsWidthScale(16)));
        make.bottom.equalTo(@(-DK_IsWidthScale(77)-DK_SafeAreaBottom));
    }];
    TTTAttributedLabel * dk_RegisterAgreementLab = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
    dk_RegisterAgreementLab.numberOfLines = 0;
    [self.view addSubview:dk_RegisterAgreementLab];
    [dk_RegisterAgreementLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dk_RegisterAgreementBtn.mas_right).offset(DK_IsWidthScale(6));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(self.dk_RegisterAgreementBtn.mas_top).offset(-DK_IsWidthScale(1));
    }];
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:@"Account login is deemed to agree with the《Privacy Agreement》and the 《Registration Agreement》"];
    [attStr addAttribute:NSFontAttributeName value:DK_FONT_Regular(12) range:NSMakeRange(0,attStr.length)];
    [attStr addAttribute:NSForegroundColorAttributeName value:DK_Hex(@"#333333") range:NSMakeRange(0,attStr.length)];
    [attStr addAttribute:NSForegroundColorAttributeName value:DK_Hex(@"#4566FF") range:NSMakeRange(41,19)];
    [attStr addAttribute:NSForegroundColorAttributeName value:DK_Hex(@"#4566FF") range:NSMakeRange(68,attStr.length-68)];
    dk_RegisterAgreementLab.delegate = self;
    dk_RegisterAgreementLab.attributedText = attStr;
    dk_RegisterAgreementLab.enabledTextCheckingTypes = NSTextCheckingTypeLink;
    dk_RegisterAgreementLab.linkAttributes = @{NSForegroundColorAttributeName:DK_Hex(@"#4566FF")};
    [dk_RegisterAgreementLab addLinkToURL:[NSURL URLWithString:DK_PrivacyAgreement] withRange:NSMakeRange(41,19)];
    [dk_RegisterAgreementLab addLinkToURL:[NSURL URLWithString:DK_RegisterAgreement] withRange:NSMakeRange(68,attStr.length-68)];
    //next
    self.dk_LoginNextBtn = [[UIButton alloc] init];
    [self.dk_LoginNextBtn setTitle:@"Next" forState:UIControlStateNormal];
    [self.dk_LoginNextBtn setTitleColor:DK_Hex(@"#4566FF") forState:UIControlStateNormal];
    [self.dk_LoginNextBtn setTitleColor:DK_Hex(@"#FFFFFF") forState:UIControlStateSelected];
    self.dk_LoginNextBtn.titleLabel.font = DK_FONT_Semibold(16);
    [self.dk_LoginNextBtn setBackgroundImage:DK_GetImage(@"dk_registerNextBg") forState:UIControlStateNormal];
    [self.dk_LoginNextBtn setBackgroundImage:DK_GetImage(@"dk_registerNextSelectBg") forState:UIControlStateSelected];
    [self.dk_LoginNextBtn addTarget:self action:@selector(goLogin) forControlEvents:UIControlEventTouchUpInside];
    self.dk_LoginNextBtn.userInteractionEnabled = false;
    [self.view addSubview:self.dk_LoginNextBtn];
    [self.dk_LoginNextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(50)));
        make.bottom.equalTo(dk_RegisterAgreementLab.mas_top).offset(-DK_IsWidthScale(100));
    }];
}
//协议弹框
- (DKLoginAgreementAlertView *)dk_LoginAgreementAlertView{
    if(_dk_LoginAgreementAlertView){
        _dk_LoginAgreementAlertView = [[DKLoginAgreementAlertView alloc] init];
    }
    return _dk_LoginAgreementAlertView;
}
- (void)showSureAgreemnetView{
    zhPopupController * sureAgreemnetCtrl = [[zhPopupController alloc] initWithView:self.dk_LoginAgreementAlertView size:CGSizeMake(DK_SCREEN_W - DK_IsWidthScale(60), DK_IsWidthScale(315))];
    sureAgreemnetCtrl.presentationStyle = zhPopupSlideStyleTransform;
    sureAgreemnetCtrl.layoutType = zhPopupLayoutTypeCenter;
    sureAgreemnetCtrl.presentationTransformScale = 0.5;
    sureAgreemnetCtrl.dismissonTransformScale = 0.85;
    sureAgreemnetCtrl.dismissOnMaskTouched = YES;
    [sureAgreemnetCtrl showInView:self.view.window completion:nil];
}
//选择协议
- (void)goAgreeAgreenment:(UIButton *)sender{
    sender.selected = !sender.isSelected;
    [self updataNextStatus];
}
- (void)updataNextStatus{
    NSString * pwd = [self.dk_resetPwdView getLoginPwd];
    if(pwd.length&&self.dk_RegisterAgreementBtn.isSelected){
        self.dk_LoginNextBtn.selected = YES;
        self.dk_LoginNextBtn.userInteractionEnabled = YES;
    }else{
        self.dk_LoginNextBtn.selected = NO;
        self.dk_LoginNextBtn.userInteractionEnabled = NO;
    }
}
- (void)goLogin{
    NSMutableDictionary *checkPwdDic = [[NSMutableDictionary alloc] init];
    [checkPwdDic setValue:self.dk_loginPhone forKey:@"loginName"];
    [checkPwdDic setValue:self.upPasswordCertificate forKey:@"upPasswordCertificate"];
    NSLog(@"密码:%@",[self.dk_resetPwdView getLoginPwd]);
    NSLog(@"公钥:%@",[DKGlobalHelper defaultManager].userRsaKey);
    NSLog(@"公钥加密：%@",[IPNRSAUtil encryptString:[self.dk_resetPwdView getLoginPwd] publicKey:[DKGlobalHelper defaultManager].userRsaKey]);
    [checkPwdDic setValue:[IPNRSAUtil encryptString:[self.dk_resetPwdView getLoginPwd] publicKey:[DKGlobalHelper defaultManager].userRsaKey] forKey:@"password"];
    [DKPublicHttp apiResetPwdForAPI:checkPwdDic complete:^{
        [DKAlertHelper showMessage:@"reset password sucess!"];
        //返回登录页面
        DKLoginViewController * dk_LoginViewCtrl = [[DKLoginViewController alloc] init];
        UINavigationController *dk_LoginViewNavCtrl = [[UINavigationController alloc] initWithRootViewController:dk_LoginViewCtrl];
        ((AppDelegate*)([UIApplication sharedApplication].delegate)).window.rootViewController = dk_LoginViewNavCtrl;
    }];
}
#pragma mark -- TTTAttributedLabelDelegate
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url{
//    DKWebViewController * dk_WebViewCtrl = [[DKWebViewController alloc] init];
//    dk_WebViewCtrl.webUrl = url.absoluteString;
    DKWebViewController * dk_WebViewCtrl = [[DKWebViewController alloc] initWithUrl:url.absoluteString withIsNavBar:YES];

    [self.navigationController pushViewController:dk_WebViewCtrl animated:YES];
}
@end
